<?php
namespace app\weapp\library;
use app\common\model\Department;
use app\common\model\Member;
use EasyWeChat\MiniApp\Application;
use Throwable;
use think\facade\Log;
class MpAppApi{
    private $mpApi;
    private $mpApp;
    private $message;
    public function __construct($apiConf = []){
        $config = [
            'app_id' => 'wxde7dc32018e45254',
            'secret' => 'b7a8f135f2f48f8275b03a1e8ec923be',
            'http' => [
                'throw'  => true, 
                'timeout' => 5.0,
                'retry' => true, // 使用默认重试配置
            ],
          ];
        $app = new Application(array_merge($config,$apiConf));
        $this->mpApp = $app;
        $this->mpApi = $app->getClient();
    }
    //通过code获取openid
    public function sessioncode($code){
        try {
            $utils = $this->mpApp->getUtils();
            $response = $utils->codeToSession($code);
            Log::info(json_encode($response,1));
            return $response['openid'];         
        } catch (\Throwable $th) {
            $this->message = $th->getMessage();
            return false;
        }
       
    }
    public function getMessage(){
        return $this->message;
    }
}